package cn.ichiva.dl.tushare.analy.impl;

import cn.ichiva.common.io.IOHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Data
public class Statistical{

    public static void writeAgent(TradeAgent agent) {
        LinkedList<Record> recordLinkedList = agent.getTrade().getTradeRecord();
        String fileName = new StringBuffer("./rpc_res/")
                .append(agent.getName())
                .append('V').append(agent.getScores().stream().map(e -> String.valueOf(e.intValue())).collect(Collectors.joining("-")))
                .append(".ai").toString();

        StringBuffer buffer = new StringBuffer("日期,价格,数量,买卖,持仓,持币,账面,佣金,税\r");
        recordLinkedList.stream()
                .filter(e -> e.getState() == 1 || e.getState() == -1)
                .forEach(e ->{
                    buffer.append(e.getDay()).append(',')
                            .append(e.getPrice()).append(',')
                            .append(e.getNumber()).append(',')
                            .append(e.getState()).append(',')
                            .append(e.getNStock()).append(',')
                            .append(e.getMoney()).append(',')
                            .append(e.getFaceMoney()).append(',')
                            .append((int)e.getCommission()).append(',')
                            .append((int)e.getTax()).append("\r");
                });

        File dir = new File(fileName).getParentFile();
        if(!dir.exists())dir.mkdirs();

        try (Writer writer = new FileWriter(fileName + ".csv")){
            writer.write(buffer.toString());
            IOHelper.writeObject(fileName,agent);
        } catch (IOException ex) {
            log.error("保存对象失败",ex);
        }
    }
    public static void writeAgentFull(TradeAgent agent) {
        LinkedList<Record> recordLinkedList = agent.getTrade().getTradeRecord();
        String fileName = new StringBuffer("./rpc_res_full/")
                .append(agent.getName())
                .append('V').append(agent.getScores().stream().map(e -> String.valueOf(e.intValue())).collect(Collectors.joining("-")))
                .append(".ai").toString();

        StringBuffer buffer = new StringBuffer("日期,价格,数量,买卖,持仓,持币,账面,佣金,税\r");
        recordLinkedList.stream()
                .forEach(e ->{
                    buffer.append(e.getDay()).append(',')
                            .append(e.getPrice()).append(',')
                            .append(e.getNumber()).append(',')
                            .append(e.getState()).append(',')
                            .append(e.getNStock()).append(',')
                            .append(e.getMoney()).append(',')
                            .append(e.getFaceMoney()).append(',')
                            .append((int)e.getCommission()).append(',')
                            .append((int)e.getTax()).append("\r");
                });

        File dir = new File(fileName).getParentFile();
        if(!dir.exists())dir.mkdirs();

        try (Writer writer = new FileWriter(fileName + ".csv")){
            writer.write(buffer.toString());
            IOHelper.writeObject(fileName,agent);
        } catch (IOException ex) {
            log.error("保存对象失败",ex);
        }
    }
}